<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<body>
		<input type="file" name="picFieldName" id="picFieldId"  />
		<div id="upload">上传</div>
	</body>
</html>
<!-- oss 上传文件 JavaScript 库 -->
<script src="../js/jquery-3.1.0.js"></script>
<script src="http://gosspublic.alicdn.com/aliyun-oss-sdk-4.4.4.min.js"></script>
<script>
    $('#upload').click(function(){
    	uploadPic($('#picFieldId')[0],function(data){
    			console.log(data);
    		},function(res){
    			console.log(res);
    		})
    })
    function uploadPic(obj,successCallback,errorCallback){
    	console.log(obj)
    	//obj==input文件
    	//successCallback  成功回调
    	//errorCallback  失败回调
    	var client = new OSS.Wrapper({
	        region : 'oss-cn-hangzhou',
	        accessKeyId : 'LTAILUlTg4vCd0xp',
	        secure:true,
	        accessKeySecret : 'iZCIFGVD5P99Ydr7pZJcB5YRIv9nA0',
	        bucket : 'mobao-app',
	        
	    });
        var file=obj.files[0];//获取文件流
        var val= obj.value;
        var suffix = val.substr(val.indexOf("."));
        var storeAs = "video/"+time()+'/'+timestamp()+suffix;
        console.log(file.name + ' => ' + storeAs);
        client.multipartUpload(storeAs, file).then(function (result) {
        	if(result.res.status==200){
        		successCallback && successCallback(result.name);
        	};
        }).catch(function (err) {
            errorCallback && errorCallback(err)
        });
        
        function time(){		//获取当前时间 文件夹名称
	    	var time = new Date();
	        var y = time.getFullYear();
	        var m = time.getMonth()+1;
	        var d = time.getDate();
	        var h = time.getHours();
	        var mm = time.getMinutes();
	        var s = time.getSeconds();
	        console.log(y);
	        return ""+y+'-'+add0(m)+'-'+d;
	    };
	    function timestamp(){		//文件名称
	        var time = new Date();
	        var y = time.getFullYear();
	        var m = time.getMonth()+1;
	        var d = time.getDate();
	        var h = time.getHours();
	        var mm = time.getMinutes();
	        var s = time.getSeconds();
	        console.log(y);
	        return ""+y+add0(m)+add0(d)+add0(h)+add0(mm)+add0(s);
	    }
	    function add0(m){
	        return m<10?'0'+m : m;
	    }
    }
</script>